/**
 * Copyright 2009 Emory University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions: The
 * above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql;

import java.util.Calendar
import java.util.GregorianCalendar

/**
 * Append a literal constant to a StringBuilder.
 *
 * @author Mark Grand
 */
class Literal protected (val value: String, val original: Any) 
     extends StaticTextAppender(value) with SQLValueAppender {
}

/**
 * Companion object to create objects that append literal SQL constants to a StringBuilder.
 *
 * @author Mark Grand
 */
object Literal {
  def apply(value: Boolean) = new Literal(value.toString(), value)
  def apply(value: Int) = new Literal(value.toString(), value)
  def apply(value: Long) = new Literal(value.toString(), value)
  def apply(value: Float) = new Literal(value.toString(), value)
  def apply(value: Double) = new Literal(value.toString(), value)
  def apply(value: java.math.BigDecimal) = new Literal(value.toString(), value)
  def apply(value: scala.BigDecimal) = new Literal(value.toString(), value)
  def apply(value: String) = new Literal("'" + value.replace("'", "''") + "'", value)
  def apply(value: java.sql.Date) = {
    val calendar = new GregorianCalendar()
    calendar.setTime(value)
    new Literal("DATE '" + calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH) + "'",
                value)
  }
  def apply(value: java.sql.Time) = {
    val calendar = new GregorianCalendar()
    calendar.setTime(value)
    new Literal(String.format("TIME '%02d:%02d:%02d'",
                              int2Integer(calendar.get(Calendar.HOUR_OF_DAY)),
                              int2Integer(calendar.get(Calendar.MINUTE)),
                              int2Integer(calendar.get(Calendar.SECOND))), 
                value)
  }
  def apply(value: java.sql.Timestamp) = {
    val calendar = new GregorianCalendar()
    calendar.setTime(value)
    new Literal(String.format("TIMESTAMP '%d-%d-%d %d:%d:%d.%03d'",
    		    		      int2Integer(calendar.get(Calendar.YEAR)),
    		    		      int2Integer(calendar.get(Calendar.MONTH)),
    		    		      int2Integer(calendar.get(Calendar.DAY_OF_MONTH)),
    		    		      int2Integer(calendar.get(Calendar.HOUR_OF_DAY)),
    		    		      int2Integer(calendar.get(Calendar.MINUTE)),
    		    		      int2Integer(calendar.get(Calendar.SECOND)),
    		    		      int2Integer(calendar.get(Calendar.MILLISECOND))), 
    		    value)
  }
  
  /**
   * Create an SQL literal of the type implied by the given value.
   * 
   * @return the created object that encapsulates the given value.
   * @throws IllegalArgumentException 
   *      If the type of the given value is unsupported.
   */
  def apply(value: AnyRef): Literal = {
    value match {
      case s: String => apply(s)
      case dt: java.sql.Date => apply(dt)
      case tm: java.sql.Time => apply(tm)
      case ts: java.sql.Timestamp => apply(ts)
      case i: java.lang.Integer => apply(i.intValue)
      case l: java.lang.Long => apply(l.longValue)
      case d: java.lang.Double => apply(d.doubleValue)
      case f: java.lang.Float => apply(f.doubleValue)
      case b: java.lang.Boolean => apply(b.booleanValue)
      case jbd: java.math.BigDecimal => apply(jbd)
      case bd: scala.BigDecimal => apply(bd)
      case _ => {
        val valueClass = value.getClass().getCanonicalName()
        throw new IllegalArgumentException("Unsupported type of value (" + valueClass + ")for Literal: " + value.toString())
      }
    }
  }
}
